<?php
class sms_controller_admin_system extends AdminController {
	private $obj = '';
	public function send_form_action(){
		$reciver = _('reciver','');
		$this->view = 'send';
	}
	
	/**
	 * 发送短消息
	 * Enter description here ...
	 * @throws Exception
	 */
	public function send_action(){
		$reciever = _('reciever');
		$title = _('title','');
		$content = _('content','');
		$_recievers = $_reciversid = $arr_failed_user = $arr_recieverid = array();
		if(!$reciever) throw new Exception('系统消息接收人不能为空', 1);
		if(!$content) throw new Exception('系统消息内容不能为空', 1);
		$this->obj = Model::factory('sms_model_system');
		$reciever = convert_quan2ban($reciever);
		$reciever = explode(',', $reciever);
		foreach ($reciever as $v){
			if(is_numeric($v)){
				array_push($_reciversid, $v);
				if($userinfo = friends_util_cache::getUserInfo($v))
					$_recievers[$v] = $userinfo['username'];
//					array_push($_recievers, $userinfo['username']);
			}else{
				array_push($_recievers, $v);
				if($userinfo = Cache::cacheFunction(array($this->SL, 'soap_query_user_by_username'), array($v))){
					$_reciversid[$userinfo['acc_id']] = $v;
					array_push($_reciversid, $userinfo['acc_id']);
				}
			}
		}
		$_reciversid = array_unique($_reciversid);
		if($_reciversid) {
			foreach ($_reciversid as $ids){
				$sms_id = $this->obj->add(
							array(
								'senderid' => 0,
								'sender' => '[:system:]',
								'recieverid' => $ids,
								'reciever' => $_recievers[$ids],
								'content' => $content,
								'title'	=> $title,
								'target' => serialize(array('id'=>0))
							)
						);
				if (!$sms_id) {
					$this->ret = self::failure(Error::ERROR_UNKNOWN );
					array_push($arr_failed_user,$v);
				}else
					array_push($arr_recieverid,$v);
			}
		}
		else {
			throw new Exception('系统消息接收人为空', 1);
			return false;
		}
		$this->_renderRedirect(self::_makeURL('sms','system','list'));
		
	}
	/**
	 * 系统短消息列表
	 * Enter description here ...
	 */
	public function list_action(){
		$uid = _('uid',0);
		$type = _('type',0);
		$starttime = _('start','');
		$endtime = _('end','');
		$this->obj = Model::factory('sms_model_system');
		$ret = $this->obj->getSysInboxPage($uid,_('page', 1), 20,$type,$starttime,$endtime);
		$pages = $this->genPages($ret['INFO'],array('uid'=>$uid,'type'=>$type,'start'=>$starttime,'end'=>$endtime),true);
		$this->ret = self::success(array('ret'=>$ret, 'pages'=>$pages));
	}
	
	public function statistic_list_action(){
		$this->obj = Model::factory('sms_model_data');
		$page = _('page',1);
		$para = array(
				'uid' =>_('uid'),
				'min' =>_('min'),
				'max' =>_('max'),
				'start_day' => _('start'),
				'end_day' =>_('end')
			);
//		$total = '';
		$ret = $this->obj->getSmsStatistic($para,$page,20,true);
		$pages = $this->genPages($ret['INFO'],array('uid'=>_('uid'),'min'=>_('min'),'max'=>_('max'),'start_day'=>_('start'),'end_day'=>_('end')),true);
		$this->ret = self::success(array('ret'=>$ret, 'pages'=>$pages));
		$this->view = 'statistic_list';
	}
	
	
	public function daily_list_action(){
		$this->obj = Model::factory('sms_model_data');
		$page = _('page',1);
		$para = array(
				'min' =>_('min'),
				'max' =>_('max'),
				'start_day' => _('start'),
				'end_day' =>_('end')
			);
		$total = '';
		$ret = $this->obj->getSmsStatistic_daily($para,$page,20,true,$total);
		$pages = $this->genPages($ret['INFO'],array('min'=>_('min'),'max'=>_('max'),'start_day'=>_('start'),'end_day'=>_('end')),true);
		$this->ret = self::success(array('ret'=>$ret, 'pages'=>$pages));
		$this->view = 'daily_list';
	}
	
	public function delete_action(){
		$id = _('id','');
		$uid = _('uid','');
		$rtype = _('rtype','');
		$this->obj = Model::factory('sms_model_system');
		$this->ret = self::success($this->obj->removeSystem($id,$uid,$type = 0));
		if($rtype==1){
			$this->_renderJson();
		}
		else 
			$this->_renderRedirect(self::_makeURL('sms','system','list'));
	}
	
	public function verify_action(){
		
	}
}